datatype [] array; // ex. int [] array; datatype array[]; // ex. int array[]; datatype []array; // ex. int []array;
public class Main { public static void main(String[] args) { int array[]= {10,20,30}; //arrays declared and Initialized } }
// Java example of Single Dimensional array
public class Main { public static void main(String[] args) { int array[]= {10,20,30}; //arrays declare and Initialized for(int i=0;i<array.length;i++){ System.out.println(array[i]); } } }
Output:
10
20
30
for(datatype variable:array){ //body of the loop }
for (int i : array) { //body of the loop }
// Java example of array with for-each loop
public class Main { public static void main(String[] args) { int array[]= {10,20,30}; for (int arr : array) { System.out.println(arr); } } }
Output:
10
20
30
When traversing an array with a length that is negative, equal to, or greater than the array size, the Java Virtual Machine (JVM) throws an ArrayIndexOutOfBoundsException.
// Java example of ArrayIndexOutOfBoundsException
package HELLO; public class Main { public static void main(String[] args) { int array[]= {10,20,30,40,50}; //arrays declare and Initialized for(int i=0;i<=5;i++){ System.out.println(array[i]); } } }
Output:
10
20
30
40
50
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at HELLO/HELLO.Main.main(Main.java:8)
// Java example to find maximum of element of array
public class Main { static void Maximum(int arr[]) { int max=arr[0]; for(int i=1;i<arr.length;i++){ if(max<arr[i]) max=arr[i]; } System.out.println(max); } public static void main(String[] args){ int array[]= {10,20,30,40,50}; Maximum(array); } }
Output:
50
// Java example to returning Array from the Method
public class Main { public static int[] array(){ int[] array={10,20,30,40}; return array; } public static void main(String args[]) { int[] array=array(); for (int i = 0; i < array.length; i++) { System.out.print(array[i]+ " "); } } }
Output:
10
20
30
40
Since Java supports anonymous arrays, you don't need to declare an array when passing it to a method.
// Java example of anonymous array
public class Main { static void AnonymousArray(int array[]){ for(int i=0;i<array.length;i++) System.out.println(array[i]); } public static void main(String args[]){ AnonymousArray(new int[]{10,20,30,40});//sending anonymous array to method } }
Output:
10
20
30
40
datatype[][] array; // e.g int[][] array; datatype [][]array; // e.g int [][]array; datatype array[][]; // e.g int array[][]; datatype []array[]; // e.g int []array[];
int[][] array=new int[2][3]; //2 row and 3 column
// Java example of Multidimensional arrays
public class Main { public static void main(String args[]){ int[][] array={{10,20,30},{40,50,60},{70,80,90}}; for(int i=0;i<array.length;i++){ for(int j=0;j<array.length;j++){ System.out.print(array[i][j]+" "); } System.out.println(); } } }
Output:
10 20 30
40 50 60
70 80 90
A jagged array is an array of arrays where each member array can be of a different size, allowing us to create a 2-D array with variable numbers of columns in each row. These arrays are also referred to as Jagged arrays.
// Java example of Jagged Array
public class Main { public static void main(String args[]){ // Declare a 2-D array with 3 rows int array[][] = new int[3][]; //initialize and declare jagged array array[0] = new int[]{10,20,30}; array[1] = new int[]{40,50,60,70}; array[2] = new int[]{80,90,}; // printing the jagged array System.out.println("Jagged Array:\n"); for (int i=0; i<array.length; i++){ for (int j=0; j<array[i].length; j++) { System.out.print(array[i][j] + " "); } System.out.println(); } } }
Output:
Jagged Array:
40 50 60
40 50 60 70
80 90
We are able to clone the Java array because it implements the Cloneable interface. When a single-dimensional array is copied, a deep copy of the Java array is also created. This means that the actual value will be copied. However, when we copy a multidimensional array, it creates a shallow copy of the Java array, which means that the references are copied.
A "deep copy" is made when you clone a single-dimensional array, such as Object[], where the new array contains actual copies of the original array's elements rather than references.
// Java example of cloning of single-dimensional arrays
public class Main { public static void main(String args[]){ int array[] = { 10, 20, 30 }; int clonedArr[] = array.clone(); // will display false because a deep copy for a one-dimensional array is created. System.out.println(array == clonedArr); // print array for (int i = 0; i < clonedArr.length; i++) { System.out.print(clonedArr[i] + " "); } } }
Output:
false
10 20 30
A clone of a multi-dimensional array (such as Object[][]) is a "shallow copy," which means it creates only a single new array with each element array referencing an original element array, but subarrays are shared.
// Java example of cloning of multi-dimensional arrays
public class Main { public static void main(String args[]){ int array[][] = {{10,20,30}, {50,60}}; int cloneArr[][] = array.clone(); System.out.println(array == cloneArr); //print false // will print true because shallow copy is used, which means that sub-arrays are shared System.out.println(array[0] == cloneArr[0]); System.out.println(array[1] == cloneArr[1]); } }
Output:
false
true
true
The Arrays class in the java.util package is a Java Collection Framework component. This class provides static methods for creating and accessing Java arrays dynamically. It only has static methods and methods from the Object class. The methods of this class can be accessed via the class name.
public class Arrays extends Object
Arrays. <method name> ;
There are a number of static methods in the Arrays class in the java.util package that may be used to fill, sort, search, etc. in arrays. Let's now talk about the methods of this class
// Java example of sorting of arrays
import java.util.Arrays; public class Main { public static void main(String args[]){ int array[] = {10, 20, 05, 02, 30}; Arrays.sort(array); // array sort System.out.println("Sorted Array is : "+ Arrays.toString(array)); } }
Output:
Sorted Array is : [2, 5, 10, 20, 30]
Post your comment